// Copyright 2012 The golab Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package machine

import (
	"fmt"
	"net"
	"net/rpc"
)

// A Server is the RPC server that should be running on an end-system.  The
// server will receive RPC requests from Clients running on the control machine.
type Server struct {
	Host string      // Network ID (can be an IP address) that determines what interface the RPC server will use
	Port int         // RPC port to use (default is 50007)
	Loc  *Local      // Used to run commands on the local hardware
	s    *rpc.Server // RPC server
}

// Call this in a goroutine on the server.
func (m *Server) Listen() (net.Listener, error) {
	if m.Port == 0 {
		m.Port = stdRPCPort
	}
	m.Loc = NewLocal()
	rpc.Register(m.Loc)
	rpc.HandleHTTP()
	return net.Listen("tcp", fmt.Sprintf("%s:%d", m.Host, m.Port))
}
